home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_c / abbrev4 / abbrev.m < prev    next >
Text File  |  1989-11-16  |  10KB  |  236 lines

  1. ;**************************************************************************
  2. ;*                                                                                                  *
  3. ;*     If you are a previous user of ABBREV, you will need to                  *
  4. ;*     replace the autoload's that were provided with that version              *
  5. ;*     with the ones specified below.                                                  *
  6. ;*                                                                                                  *
  7. ;**************************************************************************
  8. ;*                                                                                                  *
  9. ;*      ABBREV.CB                                                                              *
  10. ;*                                                                                               *
  11. ;*      This macro provides a replacement for edit_file and read_file.  It *
  12. ;*      allows arbitrary abbreviation of drive and path combinations in      *
  13. ;*      file names (and may even be used to abbreviate full file names).      *
  14. ;*                                                                                               *
  15. ;*      The if there is a slash or backslash in the path (after column 1), *
  16. ;*      then abbrev looks up a replacement for what preceeds the              *
  17. ;*      1st slash (backslash) and passes it on to the next                      *
  18. ;*      program in the chain.     If there is no slash or backslash,              *
  19. ;*      abbrev checks for a replacement of the entire name                      *
  20. ;*      specified.  (This is for using an abbreviation for a                      *
  21. ;*      file name.)  It is not necessary to use the last                          *
  22. ;*      directory name as the abbreviation, and you may                          *
  23. ;*      supply several abbreviations for a single directory.                      *
  24. ;*                                                                                               *
  25. ;*      Finally, if you wish to pass a name through without being              *
  26. ;*      altered by abbrev, then precede it with an exclaimation point.      *
  27. ;*                                                                                               *
  28. ;*      If "foo" was the abbreviation for "c:\new\dos\foo"                      *
  29. ;*                                                                                               *
  30. ;*      then (edit_file "foo\filename") would pass on:                              *
  31. ;*                                                                                               *
  32. ;*              (edit_file "c:\new\dos\foo\filename").                              *
  33. ;*                                                                                               *
  34. ;*      The abbreviations are kept in a file called "abbrev.dat".              *
  35. ;*      This file goes in the last directory specified by your                  *
  36. ;*      BPATH environment variable.    If you would like it in a                  *
  37. ;*      different directory, then you may specify this with the                  *
  38. ;*      BABBREV environment variable.  The macro "abbrev" (invoked with      *
  39. ;*      F10) will bring the abbrev.dat file into a Brief buffer to be          *
  40. ;*      edited.                                                                                  *
  41. ;*                                                                                               *
  42. ;*      Each abbreviation is specified on 1 line of the file.                  *
  43. ;*      The line is of the form:                                                          *
  44. ;*                                                                                               *
  45. ;*      <abbreviation> <whitespace> <substitution>                                  *
  46. ;*                                                                                               *
  47. ;*      The <abbreviation> must begin in column 1.    The line                      *
  48. ;*      for the above example would be:                                                  *
  49. ;*                                                                                               *
  50. ;*      foo         c:\new\dos\foo                                                          *
  51. ;*                                                                                               *
  52. ;*      To inspire you of its usefulness, part of my current file is          *
  53. ;*      setup as follows:                                                                  *
  54. ;*                                                                                               *
  55. ;*      autoexec c:\autoexec.bat                                                          *
  56. ;*      exec      c:\autoexec.bat                                                          *
  57. ;*      config     c:\config.sys                                                              *
  58. ;*      ap         d:\pinball\apple                                                          *
  59. ;*      apple     d:\pinball\apple                                                          *
  60. ;*      util      d:\util                                                                      *
  61. ;*      u          d:\util                                                                      *
  62. ;*      uni         d:\util\uni                                                              *
  63. ;*      mine      c:\soft\brief\macros\mine                                              *
  64. ;*      m          c:\soft\brief\macros\mine                                              *
  65. ;*      macros     c:\soft\brief\macros                                                  *
  66. ;*      mac         c:\soft\brief\macros                                                  *
  67. ;*      macs      c:\soft\brief\macros                                                  *
  68. ;*      inc         c:\soft\c\include                                                      *
  69. ;*      include  c:\soft\c\include                                                      *
  70. ;*                                                                                               *
  71. ;*      To install, add the following to your initials macro                      *
  72. ;*      (after any other replacements to edit_file or read_file):              *
  73. ;*                                                                                               *
  74. ;*     (autoload "abbrev.cm" "edit_file" "read_file" "abbrev")                    *
  75. ;*      (autoload "ablookup.cm" "ab_lookup" "get_abbrev_file")                 *
  76. ;*                                                                                               *
  77. ;*      Place "abbrev.cm" and "ablookup.cm" in a BPATH directory              *
  78. ;*      (e.g. c:\brief\macros).                                                          *
  79. ;*                                                                                               *
  80. ;*      Abbrev keeps the abbreviation file in a system buffer.                  *
  81. ;*      The 1st call will create the system buffer.   Subsequent calls      *
  82. ;*      will run faster.                                                                      *
  83. ;*                                                                                               *
  84. ;*      Larry DeMar     July, 1989                                                          *
  85. ;*                                                                                               *
  86. ;*      Updated for FILE_ED3 compatability,  August, 1989.   -Led              *
  87. ;*                                                                                               *
  88. ;*      Updated to allow suffix.cb to use the utilities without loading      *
  89. ;*      replacement macros October, 1989  -Led.                                      *
  90. ;*                                                                                                  *
  91. ;**************************************************************************
  92.  
  93. (extern get_abbrev_file)
  94. (extern ab_lookup)
  95.  
  96. (replacement edit_file
  97.    (
  98.       (string e_parm)
  99.       (if (!= (inq_called) "")
  100.          (return (edit_file))
  101.       )
  102.       (if (! (get_parm 0 e_parm "File: "))
  103.          (return)
  104.       )
  105.       (edit_file (abbreviate e_parm))
  106.    )
  107. )
  108. (replacement read_file
  109.    (
  110.       (string r_parm)
  111.       (if (!= (inq_called) "")
  112.          (return (read_file))
  113.       )
  114.       (if (! (get_parm 0 r_parm "File to read: "))
  115.          (return)
  116.       )
  117.       (read_file (abbreviate r_parm))
  118.    )
  119. )
  120. ;**************************************************************************
  121. ;*                                                                                                  *
  122. ;*      ABBREVIATE                                                                              *
  123. ;*                                                                                               *
  124. ;*      This is called to expand an abbreviation if necessary.                  *
  125. ;*                                                                                               *
  126. ;*      It returns the string that should be passed on.                          *
  127. ;*                                                                                                  *
  128. ;**************************************************************************
  129. (macro abbreviate
  130.    (
  131.       (int slash_index)
  132.       (int abbrev_buffer_id)
  133.       (global abbrev_buffer_id)
  134.       (string abbreviation
  135.               file_name
  136.               first_char   ; allow leading slash or backslash as a courtesy
  137.       )
  138.       (get_parm 0 file_name)
  139.       (if (== (substr file_name 1 1) "!")   ; check for bang
  140.          (return (substr file_name 2))   ; strip it and we're done.
  141.       )
  142.       ; 
  143.       ;   As a courtesy, if the user preceeded his abbreviation
  144.       ;   with slash or backslash (from using dos too much)
  145.       ;   lookup what follows the leading seperator
  146.       ; 
  147.       ;   If you want to use that leading slash or backslash
  148.       ;   to shut off abbrev, then comment out the next 2
  149.       ;   Brief statements (next 6 lines).
  150.       ; 
  151.       (= first_char (substr file_name 1 1))    ; get 1st char
  152.       (if (|| (== first_char "/") (== first_char "\\"))
  153.          (= file_name (substr file_name 2))   ; lookup rest
  154.       ;else
  155.          (= first_char "")   ; nothing stripped
  156.       )
  157.       ; 
  158.       ;   now we have to see if there is a path separator.
  159.       ; 
  160.       (= slash_index (get_first_seperator file_name))
  161.       ;       slash_index is now zero if there are now separators
  162.       ;       and has the position of the 1st seperator if non-zero.
  163.       ; 
  164.       (if slash_index
  165.          (= abbreviation (substr file_name 1 (- slash_index 1)))
  166.       ;else
  167.          (= abbreviation file_name)
  168.       )
  169.       (if (|| (index abbreviation "*") (index abbreviation "?"))   ; for FILE_ED3 if it has a star
  170.          (= abbreviation (+ first_char abbreviation))   ; don't look up!
  171.       ;else
  172.          (if (! (ab_lookup "abbrev.dat" abbreviation abbrev_buffer_id "abbrev.$b$"))
  173.             (= abbreviation (+ first_char abbreviation))   ; not found..put 1st char back.
  174.          )
  175.          ; 
  176.          ;   now....if there was a path...we must concatonate whats beyond 1st seperator
  177.          ; 
  178.       )
  179.       (if slash_index
  180.          (= abbreviation (+ abbreviation (substr file_name slash_index)))
  181.       )
  182.       (return abbreviation)
  183.    )
  184. )
  185. ;**************************************************************************
  186. ;*                                                                                                  *
  187. ;*      ABBREV                                                                                  *
  188. ;*                                                                                               *
  189. ;*      This is called to bring the abbreviation file into the                  *
  190. ;*      editor.  It first deletes the system buffer such that                  *
  191. ;*      changes (that are written) will be used on the next                      *
  192. ;*      request.                                                                              *
  193. ;*                                                                                                  *
  194. ;**************************************************************************
  195. (macro abbrev
  196.    (
  197.       (if abbrev_buffer_id
  198.          (delete_buffer abbrev_buffer_id)
  199.       )
  200.       (= abbrev_buffer_id 0)
  201.       (edit_file (get_abbrev_file "abbrev.dat"))
  202.    )
  203. )
  204. ;**************************************************************************
  205. ;*                                                                                                  *
  206. ;*      GET_FIRST_SEPERATOR                                                                  *
  207. ;*                                                                                               *
  208. ;*      This is called to return the position of the 1st seperator              *
  209. ;*      (slash or backslash) in a file name.    It returns 0 if                  *
  210. ;*      no seperators exist, or the index of the 1st one it                      *
  211. ;*      finds.                                                                                  *
  212. ;*                                                                                                  *
  213. ;**************************************************************************
  214. (macro get_first_seperator
  215.    (
  216.       (int slash_index
  217.            bslash_index
  218.       )
  219.       (string file_name)
  220.       (get_parm 0 file_name)
  221.       (= slash_index (index file_name "/"))
  222.       (= bslash_index (index file_name "\\"))
  223.       (if (&& slash_index bslash_index)   ; if both seperators are there
  224.          (if (< bslash_index slash_index)
  225.             (= slash_index bslash_index)   ; then take the lower
  226.          ;else
  227.          )
  228.       ;else
  229.          (if bslash_index   ; is 1st zero?
  230.             (= slash_index bslash_index)   ; yep...use 2nd.
  231.          )
  232.       )
  233.       (return slash_index)
  234.    )
  235. )
  236.